<%-- 
    Document   : Deplacements
    Created on : 3 nov. 2011, 20:44:15
    Author     : Bart-Home
--%>
<jsp:useBean id="gestion" scope="application" class="gestion.GestionDeplacements"/>
<jsp:useBean id="cookies" scope="application" class="gestion.GestionCookie"/>

<%
            String methode = request.getMethod();

            String nomCookie = "DernierDeplacement";
            Cookie tmpCookie = cookies.getCookie(request.getCookies(), nomCookie);

            boolean afficherDeplacements = false;
            boolean ajouterDeplacements = false;

            if (tmpCookie == null) {
                Cookie creation = new Cookie(nomCookie, gestion.getStringSize());
                response.addCookie(creation);
                response.setStatus(204);
                tmpCookie = creation;
            }

            int nbDeplacementServeur = 0;
            int nbDeplacementClient = 0;


            //teste la méthode utilisée
            if (methode.equalsIgnoreCase("get")) {

                nbDeplacementClient = Integer.parseInt(tmpCookie.getValue());
                nbDeplacementServeur = gestion.getSize();

                afficherDeplacements = true;

                if (nbDeplacementClient < nbDeplacementServeur) {
                    afficherDeplacements = true;
                    response.addCookie(new Cookie(nomCookie, gestion.getStringSize()));
                } else //Envoie de "204 No Content" => aucun nouveau message à récupérer (par défaut : 200 OK)
                {
                    response.setStatus(204);
                }
            }
            
            else if (methode.equalsIgnoreCase("post")) {
                ajouterDeplacements = true;
                System.out.println("deplacement.jsp appelle stockage.jsp");
                response.setStatus(204);
            }

%>

<% if (ajouterDeplacements) {%> <jsp:include page="Stockage.jsp" /> <% }%>

<% if (afficherDeplacements) {%>
<%@page contentType="application/xml" pageEncoding="UTF-8"%>
<Deplacements>
    <%for (int i = gestion.getSize()-1; i < gestion.getSize(); i++) {%>
    <Deplacement>
        <dep><%= gestion.getDeplacement(i).getDep()%></dep>
        <arr><%= gestion.getDeplacement(i).getArr()%></arr>
        <couleur><%= gestion.getDeplacement(i).getCouleur()%></couleur>
        <pseudo><%= gestion.getDeplacement(i).getPseudo()%></pseudo>
    </Deplacement>
    <%}%>
</Deplacements>

<%}%>